import axios from 'axios'
import qs from 'qs'
   
import 'es6-promise'
import Vue from 'vue'
import until from './until.js'
axios.defaults.baseURL = process.env.VUE_APP_BASE_API;


import Vant, { Toast } from 'vant';
import 'vant/lib/index.css';
Vue.use(Vant);

axios.defaults.baseURL = '/request';
//
// axios.defaults.baseURL = '/prod-api';
 
export function get(url, params) {
    var signature=until.getCookie("token")
    return new Promise((resolve, reject) => {
        axios.get(url, {
            params: params,

            headers: {
                'Authorization':'Bearer '+ signature,
                'Content-Type': 'application/json',
                'X-Access-Token':signature,
            }
        }).then(res => { 
            console.log(res)
            if(res.data.code==200){
                resolve(res.data)
            } 
            else if(res.data.code==401){
                window.location.href="/#/login/loginnew"
            }
            else{
                console.log(res.data)
                resolve(res.data)
            }
        }).catch(err => {
            Toast.clear()
            resolve({code:1})
        })
    })
}
export function post(url, data) {
    var signature=until.getCookie("token")
    return new Promise((resolve, reject) => {
        axios.post(url,data,{
            headers: {
                'Authorization':'Bearer '+ signature,
                'Content-Type': 'application/json',
                'X-Access-Token':signature,
            }
        }
        ).then(res => {
            if(res.data.code==200){
                resolve(res.data)
            }
            else if(res.data.code==401){
                window.location.href="/#/login/loginnew"
            }
            else{
                resolve(res.data)
            }
        }).catch(err => {
            Toast.clear()
            resolve({code:1})
            if(err.response.status==401){
                if (process.env.NODE_ENV == "development"){
                    // window.location.href="#/login"
                }else{
                    window.location.href="#/wxlogin"
                }
                
            }
            else{
               
                
            }
           
        })
    })
}



export function postNext(url, data) {
    var signature=until.getCookie("token")
    return new Promise((resolve, reject) => {
        axios.post(url,qs.stringify(data),{
           
            headers: {
                'Authorization':'Bearer '+ signature,
                'Content-Type': 'application/x-www-form-urlencoded',
                'X-Access-Token':signature,
      
            }
        }
        ).then(res => {
          
         
            if(res.data.code==200){
                resolve(res.data)
            }
            else if(res.data.code==401){
                window.location.href="/#/login/loginnew"
            }
            else{
                resolve(res.data)
            }
        }).catch(err => {


            Toast.clear()
            resolve({code:1})
            if(err.response.status==401){
                if (process.env.NODE_ENV == "development"){
                    // window.location.href="#/login"
                }else{
                    window.location.href="#/wxlogin"
                }
                
            }
            else{
               
                
            }
           
        })
    })
}



export function put(url, data) {
    var signature=until.getCookie("token")
    return new Promise((resolve, reject) => {
        axios.put(url,data,{
           
            headers: {
                'Authorization':'Bearer '+ signature,
                'Content-Type': 'application/json',
                'X-Access-Token':signature,
             
            }
        }
        ).then(res => {
          
         
            if(res.data.code==200){
                resolve(res.data)
            }
            else if(res.data.code==401){
                window.location.href="/#/login/loginnew"
            }
            else{
                resolve(res.data)
            }
        }).catch(err => {
            resolve({code:1})
            until.delCookie("qrtoken")
            Toast.clear()
            if(err.response.status==401){
                if (process.env.NODE_ENV == "development"){
                    // window.location.href="#/login"
                }else{
                    window.location.href="#/wxlogin"
                }
            }
            else{
              
            }
           
        })
    })
}
export function upload(url, data) {
    var signature=until.getCookie("token")

    return new Promise((resolve, reject) => {
        axios.post(url, data, {
            headers: {
                'Authorization':'Bearer '+ signature,
                'X-Access-Token':signature,
                'signature':signature,
  
            }
        }
        ).then(res => {
          
            console.log(res)
            if(res.data.code==200){
                resolve(res.data)
            }
            else if(res.data.code==401){
                window.location.href="/#/login/loginnew"
            }
            else{
                resolve(res.data)
            }
        }).catch(err => {
            until.delCookie("qrtoken")
            Toast.clear()
            resolve({code:1})
            if(err.response.status==401){
                if (process.env.NODE_ENV == "development"){
                    // window.location.href="#/login"
                }else{
                    window.location.href="#/wxlogin"
                }
                
            }
            else{
               
               
            }
           
        })
    })
}
export function delFun(url,params){
    var signature=until.getCookie("token")
   
   
    return new Promise((resolve, reject) => {

     
        axios.delete(url,{
			params: params,
			headers: {
                'Authorization':'Bearer '+ signature,
                'signature':signature,
                'X-Access-Token':signature,
			}
            }).then(res => {
           
				if(res.data.code==200){
					if(res.data.data==null){
						res.data.data=[]
					}
					
					resolve(res.data)
					
				}
                else if(res.data.code==401){
                    window.location.href="/#/login/loginnew"
                }
                else{
                    resolve(res.data)
                }
			}).catch(err => {   
                resolve({code:1})
                until.delCookie("qrtoken")
                Toast.clear()
                if(err.response.status==401){
                    if (process.env.NODE_ENV == "development"){
                        // window.location.href="#/login"
                    }else{
                        window.location.href="#/wxlogin"
                    }
                }
                else{
                   
                   
                }
               
            }
        )
    })
}

export function replaceUrl(url,type){
   
    return url
    
    
}


